const uniID = require('uni-id')

const register = async (event, context) => {
	const uniIDIns = uniID.createInstance({
		context
	})

	const {
		data
	} = event
	const res = await uniIDIns.register({
		username: data.username,
		password: data.password,
		gender: data.gender,
		avatar: data.avatar
	})
	return res
}

const login = async (event, context) => {
	const uniIDIns = uniID.createInstance({
		context
	})

	const {
		data
	} = event
	const res = await uniIDIns.login({
		username: data.username,
		password: data.password
	})
	return res
}

const checkLogin = async (event, context) => {
	const uniIDIns = uniID.createInstance({
		context
	})

	const res = await uniIDIns.checkToken(event.uniIdToken, {
		needUserInfo: false
	})

	return res
}


const updateInfo = async (event, context) => {

	const {
		data
	} = event

	const {
		uid,
		realName,
		birth,
		school,
		friendDetail,
		myHome,
		hobby,
		graduate
	} = data

	const db = uniCloud.database()
	// const $ = db.command
	const posts = db.collection("uni-id-users")

	const post = await posts.doc(uid)

	let res = {}


	await post.update({
		realName,
		birth,
		school,
		friendDetail,
		myHome,
		hobby,
		graduate
	})




	res.errCode = 0

	return res
}




module.exports = {
	register,
	login,
	checkLogin,
	updateInfo
}